/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.springvision.yuanfeng.weather.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.jeeplus.common.utils.excel.annotation.ExcelField;
import com.jeeplus.core.persistence.DataEntity;
import com.springvision.yuanfeng.core.SerialPortProtocol;
import com.springvision.yuanfeng.locale.entity.DevStation;

/**
 * 气象监测Entity
 * 
 * @author ycb
 * @version 2018-07-20
 */
public class Weather extends DataEntity<Weather> implements Serializable {

	private final static String _FR_PRE_TEMPERATURE = "0001";

	private final static String _FR_PRE_humidity = "0002";

	private final static String _FR_PRE_WIND_DIRECTION = "0003";

	private final static String _FR_PRE_WIND_SPEED = "0004";

	private final static String _FR_PRE_NOISE = "0005";

	private final static String _FR_PRE_PRESSURE = "0006";

	private final static String _FR_PRE_PM25 = "0007";

	private final static String _FR_PRE_PM10 = "0008";

	private static final long serialVersionUID = 1L;

	private DevStation devStation; // 所属场站

	private String temperature; // 温度

	private String humidity; // 湿度

	private String wind; // 风向

	private String speed; // 风速

	private String noise; // 噪音

	private String pressure; // 气压

	private String pm25; // pm2.5

	private String pm10; // pm10

	public final static Map<String, String> WIND_DIR_MAP = new HashMap<String, String>(); // 风向常量
	static {
		WIND_DIR_MAP.put("000f", "正北");
		WIND_DIR_MAP.put("0001", "东北");
		WIND_DIR_MAP.put("0003", "正东");
		WIND_DIR_MAP.put("0005", "东南");
		WIND_DIR_MAP.put("0007", "正南");
		WIND_DIR_MAP.put("0009", "西南");
		WIND_DIR_MAP.put("000b", "正西");
		WIND_DIR_MAP.put("000d", "西北");
		WIND_DIR_MAP.put("0000", "东北偏北");
		WIND_DIR_MAP.put("0002", "东北偏东");
		WIND_DIR_MAP.put("0004", "东南偏东");
		WIND_DIR_MAP.put("0006", "东南偏南");
		WIND_DIR_MAP.put("0008", "西南偏南");
		WIND_DIR_MAP.put("000a", "西南偏西");
		WIND_DIR_MAP.put("000c", "西北偏西");
		WIND_DIR_MAP.put("000e", "西北偏北");
	}

	private static String speedLevel(String windSpeed) {
		float speedValue = Math.round(Integer.parseInt(windSpeed, 16) / 10F) / 10f;
		// System.out.println(speedValue);
		String windLevel = "";
		if (speedValue >= 0.0f && speedValue <= 0.2f) {
			windLevel = "0级";
		}
		if (speedValue >= 0.3f && speedValue <= 1.5f) {
			windLevel = "1级";
		}
		if (speedValue >= 1.6f && speedValue <= 3.3f) {
			windLevel = "2级";
		}
		if (speedValue >= 3.4f && speedValue <= 5.4f) {
			windLevel = "3级";
		}
		if (speedValue >= 5.5f && speedValue <= 7.9f) {
			windLevel = "4级";
		}
		if (speedValue >= 8.0f && speedValue <= 10.7f) {
			windLevel = "5级";
		}
		if (speedValue >= 10.8f && speedValue <= 13.8f) {
			windLevel = "6级";
		}
		if (speedValue >= 13.9f && speedValue <= 17.1f) {
			windLevel = "7级";
		}
		if (speedValue >= 17.2f && speedValue <= 20.7f) {
			windLevel = "8级";
		}
		if (speedValue >= 20.8f && speedValue <= 24.4f) {
			windLevel = "9级";
		}
		if (speedValue >= 24.5f && speedValue <= 28.4f) {
			windLevel = "10级";
		}
		if (speedValue >= 28.5f && speedValue <= 32.6f) {
			windLevel = "11级";
		}
		if (speedValue >= 32.7f) {
			windLevel = "12级";
		}
		// System.out.println(windLevel);
		return windLevel;

	}

	public Weather() {
		super();
		this.setIdType(IDTYPE_AUTO);
	}

	public Weather(String id) {
		super(id);
	}

	/**
	 * 协议解析构造方法
	 * 
	 * @param id
	 * @param frame
	 */
	public Weather(Weather weather, String frame) {
		this.id = weather.getId();
		this.devStation = weather.getDevStation();
		this.temperature = weather.getTemperature();
		this.humidity = weather.getHumidity();
		this.wind = weather.getWind();
		this.speed = weather.getSpeed();
		this.noise = weather.getNoise();
		this.pressure = weather.getPressure();
		this.pm25 = weather.getPm25();
		this.pm10 = weather.getPm10();
//		System.out.println("frame---weather---" + frame);
		String data_length = StringUtils.substring(frame, 6, 8);
		List<String> properties = new ArrayList<String>(2);
		switch (data_length) {
		case SerialPortProtocol._FR_WEATHER_LEN1:
			properties.add(StringUtils.substring(frame, 8, 16));
			properties.add(StringUtils.substring(frame, 16, 24));
			break;
		case SerialPortProtocol._FR_WEATHER_LEN2:
			properties.add(StringUtils.substring(frame, 8, 16));
			break;
		default:
			break;
		}
		for (String pro : properties) {
			String property = StringUtils.substring(pro, 0, 4);
			String property_value = StringUtils.substring(pro, 4, 8);
			switch (property) {
			case Weather._FR_PRE_TEMPERATURE:
				this.temperature = String.valueOf(Integer.parseInt(property_value, 16) / 100F);
				break;
			case Weather._FR_PRE_humidity:
				this.humidity = String.valueOf(Integer.parseInt(property_value, 16) / 100F);
				break;
			case Weather._FR_PRE_WIND_DIRECTION:
				this.wind = WIND_DIR_MAP.get(property_value);
				break;
			case Weather._FR_PRE_WIND_SPEED:
				this.speed = speedLevel(property_value);
				break;
			case Weather._FR_PRE_NOISE:
				this.noise = String.valueOf(Integer.parseInt(property_value, 16) / 100F);
				break;
			case Weather._FR_PRE_PRESSURE:
				this.pressure = String.valueOf(Integer.parseInt(property_value, 16) / 100F);
				break;
			case Weather._FR_PRE_PM25:
				this.pm25 = String.valueOf(Integer.parseInt(property_value, 16) / 100F);
				break;
			case Weather._FR_PRE_PM10:
				this.pm10 = String.valueOf(Integer.parseInt(property_value, 16) / 100F);
				break;
			default:
				break;
			}
		}
	}

	@ExcelField(title = "所属场站", fieldType = DevStation.class, value = "devStation.name", align = 2, sort = 7)
	public DevStation getDevStation() {
		return devStation;
	}

	public void setDevStation(DevStation devStation) {
		this.devStation = devStation;
	}

	@ExcelField(title = "温度", align = 2, sort = 8)
	public String getTemperature() {
		return temperature;
	}

	public void setTemperature(String temperature) {
		this.temperature = temperature;
	}

	@ExcelField(title = "湿度", align = 2, sort = 9)
	public String getHumidity() {
		return humidity;
	}

	public void setHumidity(String humidity) {
		this.humidity = humidity;
	}

	@ExcelField(title = "风向", align = 2, sort = 10)
	public String getWind() {
		return wind;
	}

	public void setWind(String wind) {
		this.wind = wind;
	}

	@ExcelField(title = "风速", align = 2, sort = 11)
	public String getSpeed() {
		return speed;
	}

	public void setSpeed(String speed) {
		this.speed = speed;
	}

	@ExcelField(title = "噪音", align = 2, sort = 12)
	public String getNoise() {
		return noise;
	}

	public void setNoise(String noise) {
		this.noise = noise;
	}

	@ExcelField(title = "气压", align = 2, sort = 13)
	public String getPressure() {
		return pressure;
	}

	public void setPressure(String pressure) {
		this.pressure = pressure;
	}

	@ExcelField(title = "pm2.5", align = 2, sort = 14)
	public String getPm25() {
		return pm25;
	}

	public void setPm25(String pm25) {
		this.pm25 = pm25;
	}

	@ExcelField(title = "pm10", align = 2, sort = 15)
	public String getPm10() {
		return pm10;
	}

	public void setPm10(String pm10) {
		this.pm10 = pm10;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.lang.Object#toString()
	 * @author: Elvin Zhao - 279152260@qq.com
	 * @date: Created on 2018年7月31日 下午6:25:13
	 */
	@Override
	public String toString() {
		return "WeatherInfo 场站=" + devStation.getId() + "--" + devStation.getName() + " \n 气温=" + temperature + "   湿度="
				+ humidity + " \n 风向=" + wind + "   风速=" + speed + "\n 噪音=" + noise + "   气压=" + pressure + "\n pm25="
				+ pm25 + "   pm10=" + pm10;
	}

	public static void main(String[] args) {
		String s = "aa55050400040017ffff";
		Weather weather = new Weather(null, s);
		System.out.println(weather.speed);
	}
}